const cookieparser = process.server ? require("cookieparser") : undefined;

export const state = () => {
  return {
    token: null // {token: 'xxx'}
  };
};
export const mutations = {
  setToken(state, token) {
    state.token = token;
  }
};

export const actions = {
  nuxtServerInit({ commit }, { req }) {
    let token = null;
    if (req.headers.cookie) {
      const parsed = cookieparser.parse(req.headers.cookie);
      try {
        token = JSON.parse(parsed.token);
        commit("setToken", token);
      } catch (err) {
        console.warn("[store/index.js]: No valid cookie found");
      }
    }
  }
};
